<?php

/**
 * BolaoEvent form.
 *
 * @package    bolao-aabb
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfPropelFormTemplate.php 10377 2008-07-21 07:10:32Z dwhittle $
 */
class BolaoEventForm extends BaseBolaoEventForm
{
  private static $empty_date_value = '2099-12-31';

  public function configure()
  {
    unset(
      $this['created_at'], $this['updated_at']
    );

    $now = time();
    $today = $now - ( date('G', $now) * 3600 + $now % 3600 + 1);

    $this->widgetSchema['creator_id'] = new sfWidgetFormInputHidden();

    $this->widgetSchema['description'] = new sfWidgetFormInputText(array(
      'label' => 'Descri&ccedil;&atilde;o'
    ), array(
      'class' => 'large'
    ));
    $this->validatorSchema['description']->setMessage('max_length', 'O tamanho m&aacute;ximo &eacute; de %max_length% caracteres.');

    $this->widgetSchema['start_date'] = new sfWidgetFormJQueryDate(array(
      'label' => 'Data de in&iacute;cio',
      'date_widget' => new sfWidgetFormDate(array('format' => '%day%/%month%/%year%')),
      'image' => '/images/calendar.jpg',
      'culture' => 'pt-BR'
    ));
    $this->validatorSchema['start_date'] = new sfValidatorDate(array(
      'min' => $today
    ), array(
      'min' => 'A data de in&iacute;cio n&atilde;o pode ser no passado.'
    ));

    $this->widgetSchema['start_time']->setLabel('Hora de in&iacute;cio');
    $this->validatorSchema['start_time']->setMessage('required', 'Campo obrigat&oacute;rio.');

    $this->widgetSchema['end_date'] = new sfWidgetFormJQueryDate(array(
      'label' => 'Data de t&eacute;rmino',
      'date_widget' => new sfWidgetFormDate(array('format' => '%day%/%month%/%year%')),
      'image' => '/images/calendar.jpg',
      'culture' => 'pt-BR'
    ));
    $this->validatorSchema['end_date'] = new sfValidatorDate(array(
      'min' => $today,
      'required' => false,
      'empty_value' => BolaoEventForm::$empty_date_value
    ), array(
      'min' => 'A data de t&eacute;rmino n&atilde;o pode ser no passado.'
    ));

    $this->widgetSchema['end_time']->setLabel('Hora de t&eacute;rmino');

    $this->widgetSchema['location'] = new sfWidgetFormInputText(array(
      'label' => 'Local'
    ), array(
      'class' => 'large'
    ));
    $this->validatorSchema['location']->setMessage('max_length', 'O tamanho m&aacute;ximo é de %max_length% caracteres.');

    $this->widgetSchema['category_id'] = new sfWidgetFormPropelChoice(array(
      'label' => 'Categoria',
      'model' => 'BolaoCategory',
      'order_by' => array('Name', 'desc'),
      'multiple' => false,
      'expanded' => true
    ));
    $this->validatorSchema['category_id'] = new sfValidatorPropelChoice(array(
      'model' => 'BolaoCategory',
      'multiple' => false
    ));

    $this->widgetSchema['subcategory_id']->setLabel('Subcategoria');

    $this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, 'check_subcategory'))));

    $this->mergePostValidator(new sfValidatorOr(array(
      new sfValidatorSchemaCompare('start_date', sfValidatorSchemaCompare::LESS_THAN, 'end_date'),
      new sfValidatorSchemaCompare('start_date', sfValidatorSchemaCompare::EQUAL, 'end_date', array('left_field' => 'start_time', 'operator' => sfValidatorSchemaCompare::LESS_THAN_EQUAL, 'right_field' => 'end_time', 'throw_global_error' => true), array('invalid' => 'A data de t&eacute;rmino deve ser maior ou igual a data de in&iacute;cio.'))
    )));

    $this->widgetSchema['send_email'] = new sfWidgetFormInputCheckbox(array(
      'label' => 'Enviar e-mail?'
    ));
    $this->validatorSchema['send_email'] = new sfValidatorBoolean();
  }

  public function updateEndDateColumn($value)
  {
    if ($value == BolaoEventForm::$empty_date_value) return '';
    return $value;
  }
  
  function check_subcategory($validator, $values)
  {
    if (empty($values['subcategory_id'])) return $values;
    
    $subcategory = BolaoSubcategoryPeer::retrieveByPK($values['subcategory_id']);
    if ($subcategory == NULL || $subcategory->getCategoryId() != $values['category_id'])
    {
      $error = new sfValidatorError($validator, 'A subcategoria escolhida n&atilde;o pertence a categoria escolhida.');
 
      // throw an error bound to the subcategory field
      throw new sfValidatorErrorSchema($validator, array('subcategory_id' => $error));
    }

    return $values;
  }
}
